<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>画板</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <!-- <div id="canvas"></div>
    <script> 不可取，通过js操作DOM（跨线程）改样式太慢了
        canvas.onmousemove = (e) => {
            console.log(e.clientX)
            console.log(e.clientY)
            //console.log 调试打法
            let div = document.createElement('div')
            div.style.position = 'absolute'
            div.style.left = e.clientX + 'px'
            div.style.top = e.clientY + 'px'
            div.style.width = '6px'
            div.style.height = '6px'
            div.style.marginLeft = '-3px'
            div.style.marginTop = '-3px'
            div.style.borderRadius = '50%'
            div.style.backgroundColor = 'black'
            canvas.appendChild(div)
        }
    </script> -->
    <canvas id="canvas" width="100" height="100"></canvas>
    <script>
        let canvas = document.getElementById('canvas');
        canvas.width = document.documentElement.clientWidth
        canvas.height = document.documentElement.clientHeight
        let ctx = canvas.getContext("2d");
        ctx.fillStyle = "black";
        ctx.strokeStyle = 'none'
        ctx.lineWidth = 20;
        ctx.lineCap = "round";
        let last

        // dramLine(300, 300, 600, 600)
        // dramLine(600, 600, 200, 1000)

        let painting = false

        var isTouchDevice = 'ontouchstart' in
            document.documentElement;

        if (isTouchDevice) {
            canvas.ontouchstart = (e) => {
                let x = e.touches[0].clientX
                let y = e.touches[0].clientY
                last = [x, y]
            }
            canvas.ontouchmove = (e) => {
                let x = e.touches[0].clientX
                let y = e.touches[0].clientY
                dramLine(last[0], last[1], x, y)
                last = [x, y]
                // ctx.beginPath();
                // ctx.arc(x, y, 10, 0, 2 * Math.PI);
                // ctx.stroke();
                // ctx.fill();
            }

        } else {
            canvas.onmousedown = (e) => {
                painting = true
                last = [e.clientX, e.clientY]
                console.log(last)
            }

            canvas.onmousemove = (e) => {
                if (painting === true) {
                    console.log(last)
                    dramLine(last[0], last[1], e.clientX, e.clientY)
                    // ctx.beginPath();
                    // ctx.arc(e.clientX, e.clientY, 10, 0, 2 * Math.PI);
                    // ctx.stroke();
                    // ctx.fill();
                    last = [e.clientX, e.clientY]

                }
            }

            canvas.onmouseup = () => {
                painting = false
            }

        }

        function dramLine(x1, y1, x2, y2) {
            ctx.beginPath();
            ctx.moveTo(x1, y1);
            ctx.lineTo(x2, y2);
            ctx.stroke();
        }

    </script>

</body>

</html>